package datastructure.book.dp._2_linear_pre_section.single.all;

import org.junit.jupiter.api.Test;

public class _1_Solution {

    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        dp[nums.length-1] = 1;
        int result = 1;
        for (int i = nums.length-2; i >= 0; i--) {
            int maxLen = 0;
            for (int j = i+1; j < nums.length; j++) {
                if (nums[i] < nums[j]) {
                    maxLen = Math.max(maxLen,dp[j]);
                }
            }
            dp[i] = maxLen+1;
            result = Math.max(dp[i],result);
        }
        return result;
    }

    public int lengthOfLIS2(int[] nums) {
        int len = 1, n = nums.length;
        if (n == 0) {
            return 0;
        }
        int[] d = new int[n + 1];
        d[len] = nums[0];
        for (int i = 1; i < n; ++i) {
            if (nums[i] > d[len]) {
                d[++len] = nums[i];
            } else {
                int l = 1, r = len, pos = 0; // 如果找不到说明所有的数都比 nums[i] 大，此时要更新 d[1]，所以这里将 pos 设为 0
                while (l <= r) {
                    int mid = (l + r) >> 1;
                    if (d[mid] < nums[i]) {
                        pos = mid;
                        l = mid + 1;
                    } else {
                        r = mid - 1;
                    }
                }
                d[pos + 1] = nums[i];
            }
        }
        return len;
    }

    @Test
    public void test(){
        int[] ints = {0,8,4,12,2,3,4,5,6,7};
        System.out.println(lengthOfLIS2(ints));
    }
}
